package servlets1;

import beans1.Patientbean;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.Getter;
import lombok.Setter;
import service1.PatientService;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
@Getter
@Setter
@WebServlet("/Main_Patient")
public class Main_Patient extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action=request.getParameter("action");
        System.out.println("in Main.java,the action is "+action);
        if("delPatient".equals(action))
        {
            System.out.println("i am in editPatient");
            delPatient(request, response);
        }

        else if("editPatient".equals(action)) {
            System.out.println("i am in editPatient");
            try
            {
                editPatient(request, response);
            } catch (ParseException e)
            {
                throw new RuntimeException(e);
            } catch (SQLException e)
            {
                throw new RuntimeException(e);
            }
        } else if ("insertPatient".equals(action)) {
            try {
                System.out.println("i am in insertPatient");
                insertPatient(request,response);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        } else
            System.out.println("i am in showlist");
            showlist(request, response);
    }

    private void insertPatient(HttpServletRequest req, HttpServletResponse response) throws SQLException, IOException, ParseException {
        PatientService patientService=new PatientService();
        System.out.println("here");
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        String idNumber = req.getParameter("idNumber");
        String sex = req.getParameter("sex");
        String birthDate = req.getParameter("birthDate");
        String telNumber = req.getParameter("telNumber");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(birthDate);
        Patientbean str=new Patientbean(account,password,name,idNumber,sex,date,telNumber);
        System.out.println("success out str");
        patientService.insertPatient(str);
        response.sendRedirect(req.getContextPath()+"/Main_Patient");
    }

    private void editPatient(HttpServletRequest req, HttpServletResponse response) throws ParseException, IOException, SQLException {
        PatientService patientService=new PatientService();

        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String name = req.getParameter("name");
        String idNumber = req.getParameter("idNumber");
        String sex = req.getParameter("sex");
        String birthDate = req.getParameter("birthDate");
        String telNumber = req.getParameter("telNumber");
        System.out.println(telNumber);
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        Date date = ft.parse(birthDate);
        Patientbean str=new Patientbean(account,password,name,idNumber,sex,date,telNumber);
        patientService.editPatient(str);
        response.sendRedirect(req.getContextPath()+"/Main_Patient");
    }

    private void delPatient(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PatientService patientService=new PatientService();
        Patientbean str=new Patientbean();
        str.setAccount(request.getParameter("account"));
        int result=patientService.delPatient(str);
        if(result==1) {
            System.out.println("success");
        }
        else {
            System.out.println("false");
        }
//        pageContext.request.contextPath;

        response.sendRedirect(request.getContextPath()+"/Main_Patient");
        return;
    }

    private void showlist(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        PatientService patientService=new PatientService();
        ArrayList<Patientbean> Patientbeans = patientService.browse();
        request.setAttribute("list", Patientbeans);
        request.getRequestDispatcher("admin/patientManage.jsp").forward(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
